package com.bwie.service.impl;

import cn.hutool.core.io.file.FileNameUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.pojo.TbType;
import com.bwie.service.TbTypeService;
import com.bwie.mapper.TbTypeMapper;
import com.bwie.utils.R;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
* @author andy
* @description 针对表【tb_type(商品分类表)】的数据库操作Service实现
* @createDate 2025-06-20 15:27:42
*/
@Service
public class TbTypeServiceImpl extends ServiceImpl<TbTypeMapper, TbType>
    implements TbTypeService{

    @Autowired
    TbTypeMapper tbTypeMapper;

    @Autowired
    FastFileStorageClient storageClient;

    @Override
    public R getTypeTree() {
        //--1 查询所有分类
        List<TbType> listAll = tbTypeMapper.selectList(null);

        //--2 构造树形结构
        List<Tree<String>> treeList = TreeUtil.build(listAll, "0", (type, node) -> {
            node.setId(type.getTypeId().toString());
            node.setParentId(type.getTypePid().toString());
            node.setName(type.getTypeName());

            node.putExtra("typeStatus", type.getTypeStatus());
            node.putExtra("typeImage", type.getTypeImage());
            node.putExtra("typeTop", type.getTypeTop());
        });

        return R.ok().setData(treeList);
    }

    @Override
    public R addType(TbType tbType) {

        //--1 名称检查，确保唯一
        TbType type = tbTypeMapper.selectOne(
                new QueryWrapper<TbType>().lambda().eq(TbType::getTypeName, tbType.getTypeName())
        );
        if(type != null) {
            return R.error().setMsg("名称已存在");
        }

        //--2 添加
        tbTypeMapper.insert(tbType);

        return R.ok();
    }

    @Override
    public R uploadTypeImage(MultipartFile file) {

        try {
            String extName = FileNameUtil.extName(file.getOriginalFilename());
            StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(), extName, null);
            String url = "http://192.168.80.192:8888/" + storePath.getFullPath();
            return R.ok().setData(url);
        } catch (IOException e) {
            //throw new RuntimeException(e);
            e.printStackTrace();
        }

        return R.error().setMsg("上传失败");
    }

    @Override
    public R getSelectTypeTree() {
        //--1 查询所有分类
        List<TbType> listAll = tbTypeMapper.selectList(null);

        //--2 构造树形结构
        List<Tree<String>> treeList = TreeUtil.build(listAll, "0", (type, node) -> {
            node.setId(type.getTypeId().toString());
            node.setParentId(type.getTypePid().toString());
            node.setName(type.getTypeName());

            node.putExtra("label", type.getTypeName());
            node.putExtra("value", type.getTypeId().toString());
        });

        return R.ok().setData(treeList);
    }
}




